
/***********************************************************************************************************************
**                                                    Includes                                                        **
***********************************************************************************************************************/
#include "Alarm.h"

/***********************************************************************************************************************
**                                          Global Functions Definitions                                              **
***********************************************************************************************************************/
extern void Task_1ms(void);
extern void Task_5ms(void);
extern void Task_10ms(void);
extern void Task_50ms(void);
extern void Task_100ms(void);

/***********************************************************************************************************************
**                                            Private Macro Definitions                                               **
***********************************************************************************************************************/
#define ALARM_COUNTER_TICK_BASE_MS          (1u)
#define ALARM_TICKS2MS(ticks)               ((ticks) * ALARM_COUNTER_TICK_BASE_MS)
#define ALARM_MS2TICKS(ms)                  ((ms) / ALARM_COUNTER_TICK_BASE_MS)

/***********************************************************************************************************************
**                                            Private Type Definitions                                                **
***********************************************************************************************************************/
typedef struct
{
	uint16 cycle; /* 周期，单位ms */
    uint16 offset; /* 起始偏移 */    
} AlarmCfgType;

typedef struct
{
	uint16 tickCount; /* 心跳计数 */
    boolean startFlag; /* 启动标志，TRUE：定时器启动 */
	boolean setFlag; /* 定时标志，TRUE：定时周期已到 */
} AlarmType;

/***********************************************************************************************************************
**                                           Private Variable Definitions                                             **
***********************************************************************************************************************/
static AlarmCfgType AlarmCfg_1ms   = {1, 0};
static AlarmCfgType AlarmCfg_5ms   = {5, 1};
static AlarmCfgType AlarmCfg_10ms  = {10, 3};
static AlarmCfgType AlarmCfg_50ms  = {50, 4};
static AlarmCfgType AlarmCfg_100ms = {100, 7};

static AlarmType Alarm_1ms   = {0, FALSE, FALSE};
static AlarmType Alarm_5ms   = {0, FALSE, FALSE};
static AlarmType Alarm_10ms  = {0, FALSE, FALSE};
static AlarmType Alarm_50ms  = {0, FALSE, FALSE};
static AlarmType Alarm_100ms = {0, FALSE, FALSE};

/***********************************************************************************************************************
**                                           Private Function Declarations                                            **
***********************************************************************************************************************/


/***********************************************************************************************************************
**                                            Global Function Definitions                                             **
***********************************************************************************************************************/
/**
  * @brief  基于1ms定时器驱动Alarm
  * @param  None
  * @retval None
  */
void Alarm_Counter_1ms(void)
{
    /*1ms Alarm*/
    if (FALSE == Alarm_1ms.startFlag)
    {
        Alarm_1ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_1ms.tickCount) == AlarmCfg_1ms.offset)
        {
            Alarm_1ms.tickCount = 0;
            Alarm_1ms.startFlag = TRUE;
            Alarm_1ms.setFlag = TRUE;
        }
    }
    else
    {
        Alarm_1ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_1ms.tickCount) == AlarmCfg_1ms.cycle)
        {
            Alarm_1ms.setFlag = TRUE;
            Alarm_1ms.tickCount = 0;
        }
    }
    
    /*5ms Alarm*/
    if (FALSE == Alarm_5ms.startFlag)
    {
        Alarm_5ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_5ms.tickCount) == AlarmCfg_5ms.offset)
        {
            Alarm_5ms.tickCount = 0;
            Alarm_5ms.startFlag = TRUE;
            Alarm_5ms.setFlag = TRUE;
        }
    }
    else
    {
        Alarm_5ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_5ms.tickCount) == AlarmCfg_5ms.cycle)
        {
            Alarm_5ms.setFlag = TRUE;
            Alarm_5ms.tickCount = 0;
        }
    }
    
    /*10ms Alarm*/
    if (FALSE == Alarm_10ms.startFlag)
    {
        Alarm_10ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_10ms.tickCount) == AlarmCfg_10ms.offset)
        {
            Alarm_10ms.tickCount = 0;
            Alarm_10ms.startFlag = TRUE;
            Alarm_10ms.setFlag = TRUE;
        }
    }
    else
    {
        Alarm_10ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_10ms.tickCount) == AlarmCfg_10ms.cycle)
        {
            Alarm_10ms.setFlag = TRUE;
            Alarm_10ms.tickCount = 0;
        }
    }
		
    /*50ms Alarm*/
    if (FALSE == Alarm_50ms.startFlag)
    {
        Alarm_50ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_50ms.tickCount) == AlarmCfg_50ms.offset)
        {
            Alarm_50ms.tickCount = 0;
            Alarm_50ms.startFlag = TRUE;
            Alarm_50ms.setFlag = TRUE;
        }
    }
    else
    {
        Alarm_50ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_50ms.tickCount) == AlarmCfg_50ms.cycle)
        {
            Alarm_50ms.setFlag = TRUE;
            Alarm_50ms.tickCount = 0;
        }
    }
		
    /*100ms Alarm*/
    if (FALSE == Alarm_100ms.startFlag)
    {
        Alarm_100ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_100ms.tickCount) == AlarmCfg_100ms.offset)
        {
            Alarm_100ms.tickCount = 0;
            Alarm_100ms.startFlag = TRUE;
            Alarm_100ms.setFlag = TRUE;
        }
    }
    else
    {
        Alarm_100ms.tickCount++;
        if (ALARM_TICKS2MS(Alarm_100ms.tickCount) == AlarmCfg_100ms.cycle)
        {
            Alarm_100ms.setFlag = TRUE;
            Alarm_100ms.tickCount = 0;
        }
    }
}

/**
  * @brief  检查Alarm是否到期，并执行对应周期任务
  * @param  None
  * @retval None
  */
void Alarm_CheckAlarm(void)
{
    /*1ms Alarm*/
    if (TRUE == Alarm_1ms.setFlag)
    {
        Alarm_1ms.setFlag = FALSE;
        Task_1ms();
    }
    
    /*5ms Alarm*/
    if (TRUE == Alarm_5ms.setFlag)
    {
        Alarm_5ms.setFlag = FALSE;
        Task_5ms();
    }
    
    /*10ms Alarm*/
    if (TRUE == Alarm_10ms.setFlag)
    {
        Alarm_10ms.setFlag = FALSE;
        Task_10ms();
    }
    
		/*50ms Alarm*/
    if (TRUE == Alarm_50ms.setFlag)
    {
        Alarm_50ms.setFlag = FALSE;
        Task_50ms();
    }
		
    /*100ms Alarm*/
    if (TRUE == Alarm_100ms.setFlag)
    {
        Alarm_100ms.setFlag = FALSE;
        Task_100ms();
    }
}

/***********************************************************************************************************************
**                                           Private Function Definitions                                             **
***********************************************************************************************************************/



/***********************************************************************************************************************
**                                                  General Notes                                                     **
***********************************************************************************************************************/

